package com.isdepci.project.carbon.utils;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.ProductCarbon;
import com.isdepci.project.carbon.domain.ProductCarbonConsumables;
import com.isdepci.project.carbon.domain.ProductCarbonStage;
import com.isdepci.project.carbon.mapper.ProductCarbonConsumablesMapper;
import com.isdepci.project.carbon.mapper.ProductCarbonMapper;
import com.isdepci.project.carbon.mapper.ProductCarbonStageMapper;
import com.isdepci.project.factor.domain.*;
import com.isdepci.project.factor.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Component
public class ProductCarbonUtils {
    @Autowired
    private ProductCarbonMapper productCarbonMapper;
    @Autowired
    private ProductStageMapper productStageMapper;
    @Autowired
    private ProductCarbonMapper standardStageMapper;
    @Autowired
    private ProductCarbonConsumablesMapper productCarbonConsumablesMapper;
    @Autowired
    private ProductConsumablesMapper productConsumablesMapper;
    @Autowired
    private ProductCarbonStageMapper productCarbonStageMapper;

    /**
     * 删除当前批次的 ProductCarbonStage，ProductCarbonConsumables
     * 导入其他批次的 ProductCarbonStage，ProductCarbonConsumables
     */
    @Transactional(rollbackFor = Exception.class)
    public String downOtherProductCarbon(Long nowId,Long otherId) {
        ProductCarbon nowProduct = productCarbonMapper.selectProductCarbonById(nowId);
        if(nowProduct==null){
            return "未找到当前数据信息！";
        }
        //删除当前批次的 ProductCarbonStage，ProductCarbonConsumables
        productCarbonStageMapper.deleteListByProductCarbonId(nowId);
        productCarbonConsumablesMapper.deleteListByProductCarbonId(nowId);
        //查询当前批次的  ProductCarbonStage，ProductCarbonConsumables  新增
        List<ProductCarbonStage> oldStages = productCarbonStageMapper.selectStageListByProductCarbonId(otherId);

        List<Long> oldIds = new ArrayList<>();
        for (ProductCarbonStage standardStage : oldStages) {
            oldIds.add(standardStage.getId());
            standardStage.setId(null);
            standardStage.setProductCarbonId(nowId);
        }
        productCarbonStageMapper.insertList(oldStages);
        List<Long> newIds = oldStages.stream().map(ProductCarbonStage::getId).collect(Collectors.toList());
        for (ProductCarbonStage productStage : oldStages) {
            if(productStage.getParentId()==0){
                continue;
            }
            productStage.setParentId(newIds.get(oldIds.indexOf(productStage.getParentId())));
        }
        productCarbonStageMapper.updateList(oldStages);
        List<ProductCarbonConsumables> oldCarbonConsumables = productCarbonConsumablesMapper.selectConsumablesListByProductCarbonId(otherId);
        if(oldCarbonConsumables.size()<=0){
            return null;
        }
        for (ProductCarbonConsumables consumable : oldCarbonConsumables) {
            consumable.setStageId(newIds.get(oldIds.indexOf(consumable.getStageId())));
            consumable.setProductCarbonId(nowId);
        }
        productCarbonConsumablesMapper.insertList(oldCarbonConsumables);
        return null;
    }

    /**
     * 新增碳足迹时候,拉取产品库，参数
     */
    public void genProductCarbonDataByInsert(ProductCarbon carbon){
        if(carbon.getProductId()==null){
            return;
        }
        List<ProductStage>  stages = productStageMapper.selectStageListByProductId(carbon.getProductId());
        if(stages.size()<=0){
            return;
        }
        List<Long> oldIds = new ArrayList<>();
        List<ProductCarbonStage> productStages = new ArrayList<>();
        for (ProductStage standardStage : stages) {
            productStages.add(new ProductCarbonStage(carbon.getId(),standardStage));
            oldIds.add(standardStage.getId());
        }
        productCarbonStageMapper.insertList(productStages);
        List<Long> newIds = productStages.stream().map(ProductCarbonStage::getId).collect(Collectors.toList());
        for (ProductCarbonStage productStage : productStages) {
            if(productStage.getParentId()==0){
                continue;
            }
            productStage.setParentId(newIds.get(oldIds.indexOf(productStage.getParentId())));
        }
        productCarbonStageMapper.updateList(productStages);
        //获取产品的周期
        List<ProductConsumables> consumables = productConsumablesMapper.selectConsumablesListByProductId(carbon.getProductId());
        if(consumables.size()<=0){
            return;
        }
        List<ProductCarbonConsumables> insertConsumables = new ArrayList<>();
        for (ProductConsumables consumable : consumables) {
            if(oldIds.indexOf(consumable.getStageId())<0){
                continue;
            }
            insertConsumables.add(new ProductCarbonConsumables(carbon.getId(),newIds.get(oldIds.indexOf(consumable.getStageId())),consumable));
        }
        productCarbonConsumablesMapper.insertList(insertConsumables);
    }

    /**
     * 删除碳足迹相关
     */
    public AjaxResult deleteProductCarbonAllByIds(Long[] ids) {
        int row = productCarbonMapper.deleteProductCarbonByIds(ids);
        if(row>0){
            productCarbonStageMapper.deleteListByProductCarbonIds(ids);
            productCarbonConsumablesMapper.deleteListByProductCarbonIds(ids);
            return AjaxResult.success();
        }
        return  AjaxResult.error();
    }



}
